package com.hnichr.ump.talent.library.provider.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hnichr.ump.common.talent.library.entity.PersonalInfo;
import com.hnichr.ump.common.talent.library.entity.Resume;
import com.hnichr.ump.common.talent.library.service.PersonalInfoService;
import com.hnichr.ump.common.talent.library.service.ResumeService;
import com.hnichr.ump.talent.library.provider.SyncPolicyProvider;
import com.mxpioframework.jpa.JpaUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Component
public class PersonalInfoSyncPolicyProvider implements SyncPolicyProvider {

    private static final String KEY = "piInfoList";

    @Autowired
    private PersonalInfoService personalInfoService;

    @Autowired
    private ResumeService resumeService;

    @Override
    public boolean support(JSONObject jsonObject) {
        return jsonObject.containsKey(KEY);
    }

    @Override
    @Transactional
    public void sync(String syncDate, JSONObject jsonObject) {
        JSONArray piInfoList = jsonObject.getJSONArray(KEY);
        if(CollectionUtils.isEmpty(piInfoList)){
            return;
        }
        for(Object piInfoObject:piInfoList){
            if(piInfoObject instanceof JSONObject){
                JSONObject personal = (JSONObject) piInfoObject;
                PersonalInfo personalInfo = new PersonalInfo();
                personalInfo.setId(personal.getString("id"));
                personalInfo.setDelFlag(personal.getBoolean("delFlag"));
                personalInfo.setUserId(personal.getString("userId"));
                if(personal.containsKey("createTime")){
                    personalInfo.setOrginalCreateTime(new Date(personal.getLong("createTime")));
                }
                if(personal.containsKey("updateTime")){
                    personalInfo.setOrginalUpdateTime(new Date(personal.getLong("updateTime")));
                }

                personalInfo.setNation(personal.getString("r2"));
                personalInfo.setHeight(personal.getString("r3"));
                personalInfo.setWeight(personal.getString("r4"));
                personalInfo.setDriverLicense(personal.getString("r5"));
                personalInfo.setEmergencyContact(personal.getString("r7"));
                personalInfo.setEmergencyPhone(personal.getString("r8"));
                personalInfo.setUsername(personal.getString("username"));
                personalInfo.setEmail(personal.getString("email"));
                personalInfo.setSex(personal.getInteger("sex"));
                if(personal.containsKey("birthday")){
                    personalInfo.setBirthday(new Date(personal.getLong("birthday")));
                }
                personalInfo.setHomeTown(personal.getString("homeTown"));
                personalInfo.setLiveCity(personal.getString("liveCity"));
                personalInfo.setAddress(personal.getString("address"));
                personalInfo.setIdEntity(personal.getInteger("idEntity"));
                if(personal.containsKey("jobTime")){
                    personalInfo.setJobTime(new Date(personal.getLong("jobTime")));
                }
                personalInfo.setPhone(personal.getString("phone"));
                personalInfo.setPolitical(personal.getString("political"));
                if(personal.containsKey("partyTime")){
                    personalInfo.setPartyTime(new Date(personal.getLong("partyTime")));
                }
                personalInfo.setWorkYears(personal.getInteger("workYears"));
                personalInfo.setMaritalStatus(personal.getInteger("maritalStatus"));
                personalInfo.setWorkState(personal.getInteger("workState"));
                personalInfo.setIdNumber(personal.getString("idNumber"));
                personalInfo.setName(personal.getString("name"));
                personalInfo.setRoleType(personal.getString("roleType"));
                personalInfo.setBanFlag(personal.getBoolean("banFlag"));
                personalInfo.setStationName(personal.getString("stationName"));
                personalInfo.setNickName(personal.getString("nickName"));
                personalInfo.setJobFlag(personal.getInteger("jobFlag"));
                personalInfo.setEnRoleType(personal.getInteger("enRoleType"));
                personalInfo.setForeignLanguage(personal.getString("foreignLanguage"));
                personalInfo.setComputerSkill(personal.getString("computerSkill"));
                personalInfo.setSyncDate(syncDate);
                personalInfoService.merge(personalInfo);
            }
        }
    }

    @Override
    @Transactional
    public void postSync(String syncDate){
        //是否河南人
        List<PersonalInfo> personalInfoList = personalInfoService.getBySyncDate(syncDate);
        if(CollectionUtils.isEmpty(personalInfoList)){
            return;
        }

        for(PersonalInfo personalInfo:personalInfoList){
            List<Resume> resumeList = resumeService.getByUserId(personalInfo.getUserId());
            if(CollectionUtils.isEmpty(resumeList)){
                continue;
            }
            for(Resume resume:resumeList){
                if(StringUtils.equals(resume.getSyncDate(),syncDate)){
                    continue;
                }
                if(StringUtils.startsWith(personalInfo.getHomeTown(),"410000")){
                    resume.setHeNanRen("1");
                }
                else{
                    resume.setHeNanRen("0");
                }
                resume.setName(personalInfo.getName());
                JpaUtil.update(resume);
            }
        }
    }
}
